<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/8
 * Time: 9:38
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('lunbo')->count();// 获取总条数
            $lists = Db::name('lunbo')
                ->field('lunbo_id, lunbo_title, lunbo_image, lunbo_time, lunbo_sort, lunbo_sta, lunbo_type')
                ->page($Nowpage,$limits)
                ->order('lunbo_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $data = [
                'lunbo_title' => trim(input('lunbo_title'), ' '),
                'lunbo_image' => input('lunbo_image'),
                'lunbo_content' => input('lunbo_content'),
                'lunbo_type' => input('lunbo_type'),
                'lunbo_time' => date('Y-m-d H:i:s'),
                'lunbo_status' => input('lunbo_status'),
                'lunbo_type_tz' => input('lunbo_type_tz'),
            ];
            if(input('lunbo_status') == 2){
                $data['lunbo_shop'] = input('good_id');
            }
            if(input('lunbo_status') == 3){
                $data['lunbo_shop'] = input('lunbo_shop');
                $aa = Db::table('up_notice')->where('n_id',input('lunbo_shop'))->find();
                $data['lunbo_notice'] = $aa['n_type'];
            }
            $res = Db::table('up_lunbo')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $goods = Db::table('up_goods')
            ->where('good_status', 1)
            ->order('good_create_time desc')
            ->select();
        $notice = Db::table('up_notice')
            ->where('n_status', 1)
            ->where('n_del', 1)
            ->select();
        $this->assign([
            'goods' => $goods
        ]);
        $this->assign([
            'notice' => $notice
        ]);
        return $this->fetch('banner/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('lunbo_id');
            $data = [
                'lunbo_title' => trim(input('lunbo_title'), ' '),
                'lunbo_content' => input('lunbo_content'),
                'lunbo_type' => input('lunbo_type'),
                'lunbo_status' => input('lunbo_status'),
                'lunbo_type_tz' => input('lunbo_type_tz'),
            ];
            if(!empty(input('lunbo_image'))){
             $data['lunbo_image'] = input('lunbo_image');
            }
            if(input('lunbo_status') == 2){
                $data['lunbo_shop'] = input('good_id');
                $data['lunbo_notice'] = null;
            }
            if(input('lunbo_status') == 1){
                $data['lunbo_shop'] = null;
                $data['lunbo_notice'] = null;
            }

            if(input('lunbo_status') == 3){
                $data['lunbo_shop'] = input('lunbo_shop');
                $aa = Db::table('up_notice')->where('n_id',input('lunbo_shop'))->find();
                $data['lunbo_notice'] = $aa['n_type'];
            }

            $res = Db::table('up_lunbo')->where('lunbo_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $lunbo_id = input('lunbo_id');
        $lunbo = Db::table('up_lunbo')
            ->where('lunbo_id', $lunbo_id)
            ->find();
        $goods = Db::table('up_goods')
            ->where('good_status', 1)
            ->order('good_create_time desc')
            ->select();
        $this->assign([
            'lunbo' => $lunbo,
            'goods' => $goods,
            'lunbo_id' => $lunbo_id
        ]);
        $notice = Db::table('up_notice')
            ->where('n_status', 1)
            ->where('n_del', 1)
            ->select();
        $this->assign([
            'notice' => $notice
        ]);
        return $this->fetch('banner/banneredit');
    }
    
    /**
     * 轮播图详情
     * @return mixed
     */
    public function bannerDetail(){
        $lunbo_id = input('lunbo_id');
        $lunboInfo = Db::table('up_lunbo')
            ->where('lunbo_id', $lunbo_id)
            ->field('lunbo_title, lunbo_image, lunbo_content, lunbo_time')
            ->find();
        $this->assign([
            'info' => $lunboInfo
        ]);
        return $this->fetch('banner/bannerdetail');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        if(request()->isPost ()){
            $id = input('id');
            $param = [
                'lunbo_sort'=>input('value'),
            ];
            $res = Db::name('lunbo')->where('lunbo_id',$id)->update($param);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' =>' 修改失败']);
            }
        }
    }
    
    public function bannerStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择轮播图']);
        }
        $status = Db::table('up_lunbo')->where('lunbo_id', 'IN', $ids)->column('lunbo_sta');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选轮播图中含有上架中轮播图，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选轮播图中含有已下架轮播图，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_lunbo')->where('lunbo_id', 'IN', $ids)->setField('lunbo_sta', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}